<?
	// student profile
	$fullname = strtoupper($stud_data->lastname) . ', ' . ucwords($stud_data->firstname) . ' ' . ucwords($stud_data->middlename);
	if(!empty($section_data))
	{
		$grade_section = $section_data->level_code . ' - ' . $section_data->section_name;
	}else
	{
		$grade_section = '';
	}
	
	$idno = $stud_data->idno;
	$birthdate = date('F d, Y', strtotime($stud_data->dob));
	$place_of_birth = ucwords($stud_data->pob);
	$parent = '';
	$parent_occupation = '';
	$parent_address = '';
	if($stud_data->mothername)
	{
		$parent = ucwords($stud_data->mothername);
		$parent_occupation = ucwords($stud_data->mother_occupation);
		$parent_address = ucwords($stud_data->parent_address);
	}elseif($stud_data->fathername)
	{
		$parent = ucwords($stud_data->fathername);
		$parent_occupation = ucwords($stud_data->father_occupation);
		$parent_address = ucwords($stud_data->parent_address);
	}else
	{
		$parent = ucwords($stud_data->guardian_name);
		$parent_address = ucwords($stud_data->guardian_address);
	}
	
	// grades
	$subject_grades = array();
	$gpa = 0;
	foreach($subj_data AS $s)
	{

		$id = $s->subj_id;
		$first = 0.00;
		$second = 0.00;
		$third = 0.00;
		$fourth = 0.00;
		$average = 0.00;
		if(isset($grade_arr[$id][0]))
		{
			$first = round($grade_arr[$id][0],2,PHP_ROUND_HALF_UP);
		}
		if(isset($grade_arr[$id][1]))
		{
			$second = round($grade_arr[$id][1],2,PHP_ROUND_HALF_UP);
		}
		if(isset($grade_arr[$id][2]))
		{
			$third = round($grade_arr[$id][2],2,PHP_ROUND_HALF_UP);
		}
		if(isset($grade_arr[$id][3]))
		{
			$fourth = round($grade_arr[$id][3],2,PHP_ROUND_HALF_UP);
		}
		
		if($first !== 0.00 && $second !== 0.00 && $third !== 0.00 && $fourth !== 0.00)
		{
			$average = ($first + $second + $third + $fourth)/4;
			$average = round($average, 2, PHP_ROUND_HALF_UP);
			$gpa += $average;
		}else
		{
			$average = '';
			$gpa = '';
		}
		$subject_grades[] = array(
							'code' => $s->subj_code,
							'description' => $s->subj_desc,
							'grades' => array('1' => $first,
											'2' => $second,
											'3' => $third,
											'4' => $fourth
										),
							'average' => $average
							);
		
	}
?>
<div class="row">
<div class="panel callout"><h6>Report Card</h6></div>
	<div id="subj_accordion">
		<h3>Student Profile</h3>
		<div>
			<?if(!empty($stud_data)):?>
				<div class="row">
					<table>
						<tbody>
							<tr>
								<td><strong>Name:</strong></td>
								<td colspan="4"><?=$fullname?><td>
							</tr>
							<tr>
								<td><strong>Grade/Level & Section:</strong></td>
								<td colspan="4"><?=$grade_section?><td>
							</tr>
							<tr>
								<td><strong>ID Number:</strong></td>
								<td><?=$idno?><td>
								<td><strong>Birthdate:</strong></td>
								<td><?=$birthdate?><td>
							</tr>
							<tr>
								<td><strong>Birthplace:</strong></td>
								<td colspan="4"><?=$place_of_birth?><td>
							</tr>
							<tr>
								<td><strong>Parent/Guardian:</strong></td>
								<td><?=$parent?><td>
								<td><strong>Occupation:</strong></td>
								<td><?=$parent_occupation?><td>
							</tr>
							<tr>
								<td><strong>Parent/Guardian Address:</strong></td>
								<td colspan="4"><?=$parent_address?><td>
							</tr>
						</tbody>
					</table>
				</div>
			<?else:?>
				<div data-alert class="alert-box alert">Oops! Can't find Student Profile.</div>
			<?endif;?>
		</div>
		<h3>Grades</h3>
		<div>
			<?if(!empty($subj_data) && !empty($grade_arr)):?>
				<div class="row">
					<table>
						<thead>
							<tr>
								<th rowspan="2" width="200">Subject</th>
								<th colspan="4">Class Standing</th>
								<th rowspan="2">C.S. Average</th>
								<th rowspan="2">Action Taken</th>
								<th rowspan="2">Credits Earned</th>
							</tr>
							<tr>
								<th>1st</th>
								<th>2nd</th>
								<th>3rd</th>
								<th>4th</th>
							</tr>
						</thead>
						<tbody>
							<?for($x = 0; $x<count($subject_grades); $x++):?>
								<tr>
									<td><?=$subject_grades[$x]['code']?></td>
									<td><?=$subject_grades[$x]['grades']['1']?></td>
									<td><?=$subject_grades[$x]['grades']['2']?></td>
									<td><?=$subject_grades[$x]['grades']['3']?></td>
									<td><?=$subject_grades[$x]['grades']['4']?></td>
									<td><?=$subject_grades[$x]['average']?></td>
									<td></td>
									<td></td>
								</tr>
							<?endfor;?>
								<tr>
									<td colspan="3"><center>Grade Point Average:</center></td>
									<? 
										$out = '<td colspan="5"><center><strong>';
										if($gpa !== '')
										{
											$gpa = $gpa / $gp_count;
											$gpa = round($gpa, 2, PHP_ROUND_HALF_UP);
											
											if($gpa >= 90)
											{
												$out .= 'A';
											}elseif($gpa >= 85)
											{
												$out .= 'P';
											}elseif($gpa >= 80)
											{
												$out .= 'AP';
											}elseif($gpa >= 75)
											{
												$out .= 'D';
											}else
											{
												$out .= 'B';
											}
										}
										$out .= '</strong></center></td>';
										echo $out
									?>
								</tr>
						</tbody>
					</table>
				</div>
			<?else:?>
				<div data-alert class="alert-box alert">Oops! Can't find Grades.</div>
			<?endif;?>
		</div>
		<h3>Attendance</h3>
		<div>
			<div class="row">
				<?if(!empty($att_data)):?>
					<table>
						<tbody>
							<tr>
								<th><th>
								<th>Jun<th>
								<th>Jul<th>
								<th>Aug<th>
								<th>Sep<th>
								<th>Oct<th>
								
							</tr>
							<tr>
								<th>School Days:</th>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
							<tr>
							<tr>
								<th>Days Present:</th>
								<td><?if(isset($att_data[6])) echo $att_data[6]?></td>
								<td><?if(isset($att_data[7])) echo $att_data[7]?></td>
								<td><?if(isset($att_data[8])) echo $att_data[8]?></td>
								<td><?if(isset($att_data[9])) echo $att_data[9]?></td>
								<td><?if(isset($att_data[10])) echo $att_data[10]?></td>
							<tr>
						</tbody>
					</table>
					<table>
						</tbody>
							<tr>
								<th></th>
								<th>Nov<th>
								<th>Dec<th>
								<th>Jan<th>
								<th>Feb<th>
								<th>Mar<th>
							</tr>
							<tr>
								<th>School Days:</th>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
							<tr>
							<tr>
								<th>Days Present:</th>
								<td><?if(isset($att_data[11])) echo $att_data[11]?></td>
								<td><?if(isset($att_data[12])) echo $att_data[12]?></td>
								<td><?if(isset($att_data[1])) echo $att_data[1]?></td>
								<td><?if(isset($att_data[2])) echo $att_data[2]?></td>
								<td><?if(isset($att_data[3])) echo $att_data[3]?></td>
							<tr>
						</tbody>
					</table>
					<table>
						<tbody>
							<tr>
								<th><th>
								<th>Apr<th>
								<th>May<th>
							</tr>
							<tr>
								<th>School Days:</th>
								<td></td>
								<td></td>
							<tr>
							<tr>
								<th>Days Present:</th>
								<td><?if(isset($att_data[4])) echo $att_data[4]?></td>
								<td><?if(isset($att_data[5])) echo $att_data[5]?></td>
							<tr>
						</tbody>
					</table>
				<?else:?>
					<div data-alert class="alert-box alert">Oops! Can't find Attendance.</div>
				<?endif;?>
			</div>
		</div>
		<h3>Adviser's Remarks</h3>
		<div>
			<?=form_open('report_card/student_report_card/' . $enrollment_id, array('class' => 'remarks_form') );?>
				<?for($i=1; $i<=$gp_count; $i++): ?>
					<div class="row">
						<?if($i == $gp):?>
							<div class="small-4 columns">
								<label>Grading <?=$i;?></label>
							</div>
							<div class="small-8 columns">
								<?if(!empty($remarks)):?>
									<?if(array_key_exists($i, $remarks)): ?>
										<p><?=$remarks[$i]?></p>
									<?else:?>
										<input type="text" name="remarks[$i]" id="remarks[$i]" class="remarks_in" />
									<?endif;?>
								<?else:?>
									<input type="text" name="remarks[$i]" id="remarks[$i]" class="remarks_in" />
								<?endif;?>
								<?break;?>
							</div>
						<?else:?>
							<div class="small-4 columns">
								<label>Grading <?=$i?></label>
							</div>
							<div class="small-8 columns">
								<?if(!empty($remarks)): ?>
									<?if(array_key_exists($i, $remarks)): ?>
										<p><?=$remarks[$i]; ?></p>
									<?endif;?>
								<?endif;?>
							</div>
						<?endif;?>
					</div>
				<?endfor;?>
			<?form_close();?>
		</div>
	</div>
</div>